<?php
/**
 * alltosun.com  index.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-1-18 下午8:42:18 $
 * $Id: index.php 169688 2016-03-11 10:08:18Z gaoa $
 */
class Action
{
    /**
     * 订单查询页面
     */
    public function __call($action = '', $params = array())
    {
        $this->search_order();
    }

   /**
    * 订单查询页面
    */
    public function search_order()
    {
        $is_error  = Request::getParam('is_error', '0');
        $error_info = Request::getParam('error_info', '');
        
        Response::assign('is_error', $is_error);
        Response::assign('error_info', $error_info);
        Response::display('m/search_order.html');
    }

    /**
     * 查询订单信息
     */
    public function order_query()
    {
        $order_no  = request::getParam('order_no', '');
        $phone_num = request::getParam('phone_num', '');

        if (!$order_no || !$phone_num) {
            $error_info = '订单号或手机号不能为空';
            return $this->error_notice($error_info);
        }

        if (!is_numeric($order_no) || !is_numeric($phone_num)) {
            $error_info = '订单号或手机号格式错误';
            return $this->error_notice($error_info);
        }

        $order_id = order_helper::order_id_decode($order_no);
        if (!is_numeric($order_id)) {
            $error_info = $order_id;
            return $this->error_notice($error_info);
        }

        $order_info = order_helper::get_order_info($order_id);
        if (!$order_info || $order_info['other_assigned_mobile'] != $phone_num) {
            $error_info = '此订单不存在';
            return $this->error_notice($error_info);
        }

        $store_id   = $order_info['store_id'];
        $store_info = store_helper::get_store($store_id);

        if(!$store_info || $store_info['status'] == 0) {
            $error_info = '商户不存在或者已删除';
            return $this->error_notice($error_info);
        }

        // 总店信息
        $user_store_info = store_helper::get_user_store($store_info['owner_id']);

        if($order_info['progress'] == 15) {
            $error_info = '订单已取消,无法查看详情';
            return $this->error_notice($error_info);
        }

        if($order_info['type'] == 5) {
            // 查询经销商信息
            $agency_store_info = store_helper::get_agency_store($store_id);
        }

        $order_info = _widget('order')->get_order_detail($order_info, $store_info['id'], $user_store_info['addon_price']);

        // 获取店铺信息
        $store_info = store_helper::get_store($order_info['store_id']);
        $store_info['store_name'] = $store_info['title'].$store_info['title_branch'];
        $order_info['store_info'] = $store_info;

        // 获取签约人员信息
        $sign_user_info = user_helper::get_user_info($store_info['sign_user_id']);
        $order_info['sign_user_info'] = $sign_user_info;
        //订单的积分
        // $order_info['score'] = score_record_helper::get_order_score($store_info['user_id'], $order_info['id']);
        $order_info['score'] = score_record_helper::score_format($order_info['price']);

        Response::assign('order_no',   $order_no);
        Response::assign('phone_num',  $phone_num);
        Response::assign('order_info', $order_info);
        Response::display('m/order_info.html');
    }

    /**
     * 错误提醒
     */
    private function error_notice($error_info)
    {
        Response::redirect('order/m/search_order.html?is_error=1&error_info='.$error_info);
    }

    /**
     * 第三方人员确认订单错误提示
     */
    private function error_info($error_info)
    {
        echo $error_info;
    }

    /**
     * 第三方人员确认订单
     */
    public function confirm_order_by_person()
    {
        $order_no  = Request::getParam('order_no');
        $phone_num = Request::getParam('phone_num');

        if (!$order_no) {
            return $this->error_info('订单ID无效');
        }
        if (!$phone_num) {
            return $this->error_info('手机号码无效');
        }

        $order_decode_id = order_helper::order_id_decode($order_no);
        if (!is_numeric($order_decode_id)) {
            return $this->error_info($order_decode_id);
        }

        $order_info = order_helper::get_order_detail($order_decode_id);
        if (!$order_info) {
            return $this->error_info('此订单不存在');
        }

        if (10 == $order_info['progress']) {
            return $this->error_info("此订单已确认收货,\n请勿重复修改同一状态");
        }

        if (15 == $order_info['progress']) {
            return $this->error_info('此订单已经取消');
        }

        $store_id   = $order_info['store_id'];
        $store_info = store_helper::get_store($store_id);
        if (!$store_info) {
            return $this->error_info('店铺不存在');
        }

        if ($phone_num != $order_info['other_assigned_mobile']) {
            return $this->error_info('无权进行此操作');
        }

        $affected = _model('order')->update($order_decode_id, array('progress'=>10));
        if ($affected) {

            $store_user_info  = user_helper::get_user_info($store_info['user_id']);
            $agency_user_info = user_helper::get_user_info($store_info['agency_id']);

            $agency_store_info = store_helper::get_agency_store($store_id);
            $agency_user_store_info = store_helper::get_user_store_info($store_info['agency_id']);
            _widget('order')->confirm_order_arrival(array(), $store_id, $store_info, $order_info, $agency_store_info, $agency_user_store_info, $agency_user_info, $order_decode_id);
            return $this->error_info('确认收货完成');
        } else {
            return $this->error_info('网络原因,请重试');
        }
    }

}
?>